package com.xiaoxx.coin.impl.futures;


import com.alibaba.fastjson.JSONObject;
import com.xiaoxx.coin.enums.HttpMethodEnum;
import com.xiaoxx.coin.handler.RequestHandler;
import com.xiaoxx.coin.po.res.*;
import com.xiaoxx.coin.po.ProxyAuth;

import java.util.LinkedHashMap;
import java.util.List;

/**
 * <h2>Trade Endpoints</h2>
 * Response will be returned in <i>String format</i>.
 */
public class Account {
    private String productUrl;
    private RequestHandler requestHandler;
    private boolean showLimitUsage;

    public Account(String productUrl, String apiKey, String secretKey, boolean showLimitUsage, ProxyAuth proxy) {
        this.productUrl = productUrl;
        this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
        this.showLimitUsage = showLimitUsage;
    }

    public String getProductUrl() {
        return this.productUrl;
    }

    public RequestHandler getRequestHandler() {
        return this.requestHandler;
    }

    public boolean getShowLimitUsage() {
        return this.showLimitUsage;
    }

    public void setProductUrl(String productUrl) {
        this.productUrl = productUrl;
    }

    public void setRequestHandler(String apiKey, String secretKey, ProxyAuth proxy) {
        this.requestHandler = new RequestHandler(apiKey, secretKey, proxy);
    }

    public void setShowLimitUsage(boolean showLimitUsage) {
        this.showLimitUsage = showLimitUsage;
    }

    private final String BALANCE = "/v3/balance";

    public List<AccountBalanceResponse> futuresAccountBalance(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, BALANCE, parameters, HttpMethodEnum.GET, showLimitUsage);
        List<AccountBalanceResponse> accountBalanceResponses = JSONObject.parseArray(s, AccountBalanceResponse.class);
        return accountBalanceResponses;
    }

    private final String ACCOUNT_INFORMATION = "/v3/account";

    /**
     * 获取账户开单信息
     *
     * @param parameters
     * @return
     */
    public AccountBillingInfoResponse accountInformation(LinkedHashMap<String, Object> parameters) {
        String s = getRequestHandler().sendSignedRequest(getProductUrl(), ACCOUNT_INFORMATION, parameters, HttpMethodEnum.GET, showLimitUsage);
        return JSONObject.parseObject(s, AccountBillingInfoResponse.class);
    }

    private final String OPEN_ORDERS = "/v1/openOrders";

    public List<CurrentAllOpenOrdersResponse> currentAllOpenOrders(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, OPEN_ORDERS, parameters, HttpMethodEnum.GET, showLimitUsage);
        return JSONObject.parseArray(s, CurrentAllOpenOrdersResponse.class);
    }

    private final String ALL_ORDERS = "/v1/allOrders";

    public List<CurrentAllOpenOrdersResponse> allOrders(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, ALL_ORDERS, parameters, HttpMethodEnum.GET, showLimitUsage);
        return JSONObject.parseArray(s, CurrentAllOpenOrdersResponse.class);
    }
    private final String CANCEL_ORDER = "/v1/order";
    public CancelOrderResponse cancelOrder(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, CANCEL_ORDER, parameters, HttpMethodEnum.DELETE, showLimitUsage);
        return JSONObject.parseObject(s, CancelOrderResponse.class);
    }

    private final String BATCH_ORDERS = "/v1/batchOrders";
    public List<CancelOrderResponse> cancelBatchOrders(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, BATCH_ORDERS, parameters, HttpMethodEnum.DELETE , showLimitUsage);
        return JSONObject.parseArray(s, CancelOrderResponse.class);
    }
    private final String ORDER = "/v1/order";
    public CommonOrderResponse newOrder(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, ORDER, parameters, HttpMethodEnum.POST, showLimitUsage);
        return JSONObject.parseObject(s, CommonOrderResponse.class);
    }

    public CommonOrderResponse queryOrder(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, ORDER, parameters, HttpMethodEnum.GET, showLimitUsage);
        return JSONObject.parseObject(s, CommonOrderResponse.class);
    }

    private final String USER_TRADES = "/v1/userTrades";
    public List<UserTradesResponse> accountTradeList(LinkedHashMap<String, Object> parameters) {
        String s = requestHandler.sendSignedRequest(productUrl, USER_TRADES, parameters, HttpMethodEnum.GET, showLimitUsage);
        return JSONObject.parseArray(s, UserTradesResponse.class);
    }
}